package com.guguskins.back.controller;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.guguskins.back.common.annotation.Authorized;
import com.guguskins.back.entity.GuUserLevel;
import com.guguskins.back.entity.vo.LevelPrizeVo;
import com.guguskins.back.entity.vo.ResultVo;
import com.guguskins.back.service.GuUserLevelService;
import com.guguskins.back.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author mybatis-plus
 * @since 2021-06-15
 */
@RestController
@RequestMapping("/userLevel")
public class GuUserLevelController {

    @Autowired
    private GuUserLevelService userLevelService;

    @GetMapping("/showListMap")
    public ResultVo showListMap(@RequestHeader("token") String token) {
        Long userId = JwtUtil.verifyToken(token).get("userId", Long.class);

        List<Map<String, Object>> result = userLevelService.showListMap(userId);

        return ResultVo.success(result);
    }

    @PostMapping("/receivePrize")
    @Authorized
    public ResultVo receivePrize(@RequestHeader("token") String token,
                                 @RequestBody GuUserLevel userLevel) {
        Long userId = JwtUtil.verifyToken(token).get("userId", Long.class);
        LevelPrizeVo levelPrizeVo = userLevelService.receivePrize(userId, userLevel.getUlId());
        return ResultVo.success(levelPrizeVo, "领取成功");
    }


    //    @PostMapping("/back/list")
//    public ResultVo backList() {
//        List<GuUserLevel> userLevels = userLevelService.list(new LambdaUpdateWrapper<GuUserLevel>() {
//            {
//                orderByAsc(GuUserLevel::getUlValue);
//            }
//        });
//        return ResultVo.success(userLevels);
//    }
    @GetMapping("/back/list")
    public ResultVo BackList() {

        return ResultVo.success(userLevelService.backList(), "查找成功");
    }

    @PostMapping("/back/update")
    public ResultVo backUpdate(@RequestBody GuUserLevel userLevel) {
        userLevel.setUlValue(null);
        userLevel.setUlExperience(null);
        userLevelService.updateById(userLevel);
        return ResultVo.success("修改成功");
    }

    @PostMapping("/back/insert")
    public ResultVo backInsert(@RequestBody GuUserLevel userLevel) {
        int count = userLevelService.insert(userLevel);
        if (count == 1) {
            return ResultVo.fail("经验值低");
        }
        if (count == 2) {
            return ResultVo.fail("等级高");
        }
        if (count == 3) {
            return ResultVo.fail("等级低");
        }

        return ResultVo.success("添加成功");
    }

    @GetMapping("back/delect")
    public ResultVo backDelect(@RequestParam(value = "id", required = false) int id) {
        userLevelService.removeById(id);
        return ResultVo.success("删除成功");
    }
}
